﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WpfControls">

    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />

    <Style TargetType="ListBoxItem"
           x:Key="SuggestionItemStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border x:Name="ContentBorder"
                            Background="White">
                        <ContentPresenter />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver"
                                 Value="True">
                            <Setter Property="Background"
                                    TargetName="ContentBorder"
                                    Value="{x:Static SystemColors.HighlightBrush}" />
                            <Setter Property="IsSelected"
                                    Value="True" />
                        </Trigger>
                        <Trigger Property="IsSelected"
                                 Value="True">
                            <Setter Property="Background"
                                    TargetName="ContentBorder"
                                    Value="{x:Static SystemColors.HighlightBrush}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type TextBox}"
           x:Key="TransparentTextBoxStyle">
        <Setter Property="FocusVisualStyle"
                Value="{x:Null}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Grid>
                        <ScrollViewer x:Name="PART_ContentHost"
                                      Background="Transparent"
                                      Focusable="True"
                                      HorizontalScrollBarVisibility="Hidden"
                                      VerticalScrollBarVisibility="Hidden" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type local:AutoCompleteTextBox}">
        <Setter Property="Focusable"
                Value="False" />
        <Setter Property="BorderThickness"
                Value="1" />
        <Setter Property="BorderBrush"
                Value="Gray" />
        <Setter Property="Validation.ErrorTemplate" 
                Value="{x:Null}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:AutoCompleteTextBox}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Background="{TemplateBinding Background}"
                            CornerRadius="3">
                        <Grid>
                            <DockPanel>
                                <ContentPresenter ContentSource="Icon"
                                                  x:Name="PART_Icon"
                                                  Visibility="{TemplateBinding IconVisibility}" />
                                <Grid>
                                    <TextBlock x:Name="PART_Watermark"
                                               Text="{TemplateBinding Watermark}"
                                               Visibility="Collapsed"
                                               VerticalAlignment="Center"
                                               HorizontalAlignment="Left"
                                               Focusable="False"
                                               Foreground="Gray"
                                               Margin="3,0" />
                                    <TextBox x:Name="PART_Editor"
                                             HorizontalAlignment="Stretch"
                                             VerticalAlignment="Center"
                                             Style="{StaticResource ResourceKey=TransparentTextBoxStyle}"
                                             Text="{Binding Path=Text, RelativeSource={RelativeSource Mode=TemplatedParent}, Mode=TwoWay}" />
                                </Grid>
                            </DockPanel>
                            <Popup x:Name="PART_Popup"
                                   IsOpen="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource Mode=TemplatedParent},Mode=TwoWay}"
                                   Width="{TemplateBinding ActualWidth}"
                                   HorizontalOffset="0"
                                   MinHeight="25"
                                   MaxHeight="600"
                                   AllowsTransparency="True"
                                   PopupAnimation="Slide"
                                   Focusable="False">
                                <Border Background="White"
                                        BorderThickness="1"
                                        BorderBrush="Gray"
                                        CornerRadius="5"
                                        Padding="2">
                                    <StackPanel Orientation="Vertical">
                                        <ContentPresenter ContentSource="LoadingContent"
                                                          Visibility="{Binding Path=IsLoading, RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource ResourceKey=BoolToVisConverter}}" />
                                        <ListBox x:Name="PART_Selector"
                                                 ItemTemplate="{TemplateBinding ItemTemplate}"
                                                 Focusable="False"
                                                 BorderThickness="0"
                                                 ItemContainerStyle="{StaticResource ResourceKey=SuggestionItemStyle}">
                                        </ListBox>
                                    </StackPanel>
                                </Border>
                            </Popup>

                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="Text"
                                 Value=""
                                 SourceName="PART_Editor">
                            <Setter Property="Visibility"
                                    Value="Visible"
                                    TargetName="PART_Watermark" />
                        </Trigger>
                        <Trigger Property="IconPlacement"
                                 Value="Left">
                            <Setter Property="DockPanel.Dock"
                                    Value="Left"
                                    TargetName="PART_Icon" />
                        </Trigger>
                        <Trigger Property="IconPlacement"
                                 Value="Right">
                            <Setter Property="DockPanel.Dock"
                                    Value="Right"
                                    TargetName="PART_Icon" />
                        </Trigger>
                        <Trigger Property="Validation.HasError" Value="True">
                            <Setter Property="BorderBrush"
                                    Value="Red"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
